package com.hidt.shop.controller;

import com.hidt.shop.domain.Browse;
import com.hidt.shop.domain.CollectionAndGoods;
import com.hidt.shop.service.BrowseService;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BrowseController {
    @Autowired
    private BrowseService browseService;
    /**
     * 添加浏览记录
     *@参数  browse
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/21
     */
    @RequestMapping("/addbrowse")
    @ResponseBody
    public Map AddBrowse(@RequestBody Map<String,String> map) {
        System.out.println(111);
        int id = Integer.parseInt(map.get("id"));
        int good_id = Integer.parseInt(map.get("good_id"));
        List<CollectionAndGoods> list = browseService.FindAllBrowseById(id);
        int state = 1;
        int a = 0;
        while (a < list.size()) {
            if (good_id == list.get(a).getGood_id()) {
                state = 0;
                break;
            }
            a++;
        }
        Browse browse =new Browse();
        browse.setId(id);
        browse.setGood_id(good_id);
        HashMap map1=new HashMap();
        if (state == 1) {
            int i =browseService.AddBrowse(browse);
            map1.put("mes","添加成功");
        } else if (state == 0) {
            map1.put("mes","添加失败");
        }
        return map1;
    }
    
    /**
     * 通过会员id删除浏览商品id的记录
     *@参数  browse
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/21
     */
    @RequestMapping("/deletebrowsebyid")
    @ResponseBody
    public Map DeleteBrowseById(@RequestBody Browse browse){
        System.out.println(browse);
        int i = browseService.DeleteBrowseById(browse);
        HashMap map=new HashMap();
        if (i>0){
            map.put("mes","删除成功");
            System.out.println("删除成功");
        }else {
             map.put("mes","删除失败");
        }
        return map;
    }

    /**
     * 通过会员id查看浏览记录
     *@参数  id
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/21
     */
    @RequestMapping("/findalbrowsebyid")
    @ResponseBody
    public Map FindAllBrowseById(@RequestBody Map<String,String> map){
        int id = Integer.parseInt(map.get("id"));
        List<CollectionAndGoods> collectList = browseService.FindAllBrowseById(id);
                System.out.println(collectList);
        for (Object list1:collectList) {
            System.out.println(list1);
        }
        HashMap map1 = new HashMap();
        map1.put("collectList",collectList);
        map1.put("mes","查看收藏");
        return map1;
    }
}
